<%@page import="org.lzy.pt.bean.Plugin"%>
<%@page import="org.lzy.pt.PluginManager"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.Map"%>
<%@page import="java.util.Collections"%>
<%@page import="java.util.List"%>
<%@page import="org.jivesoftware.openfire.session.ClientSession"%>
<%@ page import="org.jivesoftware.util.LocaleUtils,
                 org.jivesoftware.util.ParamUtils"
%><%@ page import="org.jivesoftware.util.StringUtils"%>
<%@ page import="java.net.URLEncoder" %>
<%@ page import="java.util.Collection" %>

<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/fmt_rt" prefix="fmt" %>

<%!
    final int DEFAULT_RANGE = 100;
    final int[] RANGE_PRESETS = {25, 50, 75, 100, 500, 1000, -1};
%>

<jsp:useBean id="webManager" class="org.jivesoftware.util.WebManager"  />
<% webManager.init(request, response, session, application, out ); %>
<%  // Get parameters
%>

<html>
    <head>
        <title><fmt:message key="item.plugin.summary"></fmt:message></title>
        <meta name="pageID" content="plugin-summary"/>
    </head>
    <body>
<%  // Get parameters
    int start = ParamUtils.getIntParameter(request,"start",0);
    int range = ParamUtils.getIntParameter(request,"range",webManager.getRowsPerPage("plugin-summary", DEFAULT_RANGE));

    if (request.getParameter("range") != null) {
        webManager.setRowsPerPage("plugin-summary", range);
    }

    // Get the user manager
    long userCount = PluginManager.getInstance().getCount();

    // paginator vars
    int numPages = (int)Math.ceil((double)userCount/(double)range);
    int curPage = (start/range) + 1;
%>

<p>
<fmt:message key="plugin.summary.total_plugin" />:
<b><%= LocaleUtils.getLocalizedNumber(userCount) %></b> --

<%  if (numPages > 1) { %>

    <fmt:message key="global.showing" />
    <%= LocaleUtils.getLocalizedNumber(start+1) %>-<%= LocaleUtils.getLocalizedNumber(start+range > userCount ? userCount:start+range) %>,

<%  } %>
-- <fmt:message key="plugin.summary.plugins_per_page" />:
<select size="1" onchange="location.href='plugin-summary.jsp?start=0&range=' + this.options[this.selectedIndex].value;">

    <% for (int aRANGE_PRESETS : RANGE_PRESETS) { %>

    <option value="<%  if (aRANGE_PRESETS > 0) { %><%= aRANGE_PRESETS %><%  }else{ %><%= userCount %><%}%>"
            <%= (aRANGE_PRESETS == range ? "selected" : "") %>><%  if (aRANGE_PRESETS > 0) { %><%= aRANGE_PRESETS %><%  }else{ %><%= userCount %><%}%>
    </option>

    <% } %>

</select>
</p>

<%  if (numPages > 1) { %>

    <p>
    <fmt:message key="global.pages" />:
    [
    <%  int num = 15 + curPage;
        int s = curPage-1;
        if (s > 5) {
            s -= 5;
        }
        if (s < 5) {
            s = 0;
        }
        if (s > 2) {
    %>
        <a href="plugin-summary.jsp?start=0&range=<%= range %>">1</a> ...

    <%
        }
        int i;
        for (i=s; i<numPages && i<num; i++) {
            String sep = ((i+1)<numPages) ? " " : "";
            boolean isCurrent = (i+1) == curPage;
    %>
        <a href="plugin-summary.jsp?start=<%= (i*range) %>&range=<%= range %>"
         class="<%= ((isCurrent) ? "jive-current" : "") %>"
         ><%= (i+1) %></a><%= sep %>

    <%  } %>

    <%  if (i < numPages) { %>

        ... <a href="plugin-summary.jsp?start=<%= ((numPages-1)*range) %>&range=<%= range %>"><%= numPages %></a>

    <%  } %>

    ]

    </p>

<%  } %>

<div class="jive-table">
<table cellpadding="0" cellspacing="0" border="0" width="100%">
<thead>
    <tr>
        <th>&nbsp;</th>
        <th nowrap><fmt:message key="plugin.T_Name" /></th>
        <th nowrap><fmt:message key="plugin.T_Ver" /></th>
        <th nowrap><fmt:message key="plugin.T_Path" /></th>
<%--         <th nowrap><fmt:message key="plugin.T_Data" /></th> --%>
        <th nowrap><fmt:message key="plugin.T_DateTime" /></th>
        
        <th nowrap><fmt:message key="plugin.summary.edit" /></th>
        <th nowrap><fmt:message key="global.delete" /></th>
    </tr>
</thead>
<tbody>

<%  
    Collection<Plugin> plugins = PluginManager.getInstance().getPluginList(start, range);
    if (plugins.isEmpty()) {
%>
    <tr>
        <td align="center" colspan="7">
            <fmt:message key="plugin.summary.not_plugin" />
        </td>
    </tr>

<%
    }
    int i = start;
    for (Plugin plugin : plugins) {
        i++;
%>
    <tr class="jive-<%= (((i%2)==0) ? "even" : "odd") %>">
        <td width="1%">
            <%= i %>
        </td>
        <td>
            <%= plugin.getT_CnName() %> &nbsp;
        </td>
        <td >
        	 <%= plugin.getT_Ver() %>
        </td>
        <td >
        	 <%= plugin.getT_Path()==null?"":plugin.getT_Path() %>
        </td>
<!--         <td > -->
<%--         	 <%= plugin.getT_Data()==null?"":plugin.getT_Data() %> --%>
<!--         </td> -->
        <td >
            <%= plugin.getT_DateTime(true) %>
        </td>
        
         <td width="1%" align="center">
            <a href="plugin-edit-form.jsp?T_Name=<%=plugin.getT_Name() %>&T_Ver=<%=plugin.getT_Ver() %>" title="<fmt:message key="global.click_edit" />">
            	<img src="images/edit-16x16.gif" width="16" height="16" border="0" alt="<fmt:message key="global.click_edit" />">
            </a>
        </td>
        <td width="1%" align="center" style="border-right:1px #ccc solid;">
            <a href="plugin-delete.jsp?T_Name=<%=plugin.getT_Name() %>&T_Ver=<%=plugin.getT_Ver() %>"
             title="<fmt:message key="global.click_delete" />"
             ><img src="images/delete-16x16.gif" width="16" height="16" border="0" alt="<fmt:message key="global.click_delete" />"></a>
        </td>
    </tr>

<%
    }
%>
</tbody>
</table>
</div>

<%  if (numPages > 1) { %>

    <p>
    <fmt:message key="global.pages" />:
    [
    <%  int num = 15 + curPage;
        int s = curPage-1;
        if (s > 5) {
            s -= 5;
        }
        if (s < 5) {
            s = 0;
        }
        if (s > 2) {
    %>
        <a href="plugin-summary.jsp?start=0&range=<%= range %>">1</a> ...

    <%
        }
        for (i=s; i<numPages && i<num; i++) {
            String sep = ((i+1)<numPages) ? " " : "";
            boolean isCurrent = (i+1) == curPage;
    %>
        <a href="plugin-summary.jsp?start=<%= (i*range) %>&range=<%= range %>"
         class="<%= ((isCurrent) ? "jive-current" : "") %>"
         ><%= (i+1) %></a><%= sep %>

    <%  } %>

    <%  if (i < numPages) { %>

        ... <a href="plugin-summary.jsp?start=<%= ((numPages-1)*range) %>&range=<%= range %>"><%= numPages %></a>

    <%  } %>

    ]

    </p>

<%  } %>

    </body>
</html>
